package com.example.sixtrainingdemo.view;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.GridLayoutManager;

import com.example.sixtrainingdemo.R;
import com.example.sixtrainingdemo.adapter.TalkListAdapter;
import com.example.sixtrainingdemo.adapter.UserAdapter;
import com.example.sixtrainingdemo.contract.AddTalkContract;
import com.example.sixtrainingdemo.entity.RoundPhoto;
import com.example.sixtrainingdemo.entity.Talk;
import com.example.sixtrainingdemo.presenter.AddRoundPresenter;
import com.example.sixtrainingdemo.presenter.AddTalkPresenter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

public class TalkActivity extends AppCompatActivity implements AddTalkContract.IAddTalkView, View.OnClickListener{

    private EditText mtalkEdit;
    private AddTalkContract.IAddTalkPresenter mAddTalkPresenter;
    private TextView mNihao;
    private Button mButton;
    private TalkFragment mTalkFragment;
    private Fragment mContentFragment;
    private FragmentManager mFragmentManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_talk);
        mtalkEdit = findViewById(R.id.talk_edit);
        mNihao = findViewById(R.id.nihao);
        mButton = findViewById(R.id.send);
        mButton.setOnClickListener(this);
        mAddTalkPresenter = new AddTalkPresenter(this, this);
        initFragment();
        changeFragment(mTalkFragment);
        mFragmentManager.beginTransaction()
                .add(R.id.fragment_content, mTalkFragment).commitAllowingStateLoss();
        mContentFragment = mTalkFragment;
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        mNihao.setSelected(true);
    }

    private void initFragment() {
        mFragmentManager = getSupportFragmentManager();
        mTalkFragment = new TalkFragment();
    }

    public void changeFragment(Fragment to) {
        if (mContentFragment != null && mContentFragment != to) {
            if (!to.isAdded()) {
                mFragmentManager.beginTransaction().hide(mContentFragment)
                        .add(R.id.fragment_content, to).commitAllowingStateLoss();
            } else {
                mFragmentManager.beginTransaction().hide(mContentFragment)
                        .show(to).commitAllowingStateLoss();
            }
            mContentFragment = to;
        }
    }

    @Override
    public void showTalkList(List<Talk> talkList) {
    }
//    public void updateData(List<Talk> list) {
//        mList.clear();
//        mList = list;
//        notifyDataSetChanged();
//    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.nihao) {
            changeFragment(mTalkFragment);
        } else if (id == R.id.send) {
            Talk talk = new Talk();
            Log.d("TAG", "onClick: ");
            talk.setMessage(mtalkEdit.getText().toString());
            mAddTalkPresenter.saveTalk(talk);
            Toast.makeText(this, "发送成功", Toast.LENGTH_SHORT).show();
            //刷新页面
            recreate();
            EditText editText = findViewById(R.id.talk_edit);
            editText.setText("");
        }
    }
}